<template>
  <el-dialog width="400px" title="选择分组" :visible.sync="params.visible">
    <compTree
      ref="tree"
      :tree-data="treeData"
      :comp-tree-height="350"
      :highlight-current="treeHighlightCurrent"
      :show-search="false"
      :default-expanded-keys="treeDefaultExpandedKeys"
    />
    <dialog-btn slot="footer" />
  </el-dialog>
</template>
<script>
import { getMoveGroupList } from '@/api/flow'
import Global from '@/../public/js/global'
import { ParamArr, SQLTable } from '@/../public/js/paramarr'
export default {
  name: 'SelectMoveGroup',
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false,
          id: '',
          pid: '',
          type: ''
        }
      }
    }
  },
  data() {
    return {
      checked: false,
      visible: false,
      formData: '',
      treeData: [],
      nodeRob: {},
      nodeCheck: [],
      resule: '',
      treeHighlightCurrent: true,
      treeDefaultExpandedKeys: [],
      treeNodeSelectData: ''
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.formData = this.params
      var vPar = new ParamArr()
      vPar.setStr('Type', this.formData.type)
      getMoveGroupList(vPar).then((vPar) => {
        this.lpfInitWindow(vPar)
      })
    },
    getRsturn(nodeCheck) {
      var arrID = []
      var arrIP = []
      var arrName = []
      for (var i = 0; i < nodeCheck.length; i++) {
        var node = nodeCheck[i]
        switch (node.type) {
          case 2:
            if (node.id !== '') {
              arrID.push(node.id)
              arrIP.push(node.ip)
              arrName.push(node.name)
            }
            break
        }
      }
      var obj = {}
      obj.arrID = arrID
      obj.arrIP = arrIP
      obj.arrName = arrName
      return obj
    },
    lpfInitWindow(vPar) {
      var nodes = []
      var srcNode = [] // 源分组
      // 添加顶层
      var nodeMap = new Map()
      // 分组
      var table = new SQLTable(vPar.getTable('k_group'))
      table.first()
      while (!table.eof()) {
        var pnode = Global.getTreeviewPNode(
          nodeMap,
          nodes,
          table.getValueByName('PID')
        )
        var node = {}
        // data
        const id = table.getValueByName('ID')
        node['id'] = table.getValueByName('ID')
        node['name'] = table.getValueByName('Name')
        node['text'] = node.name
        node['icon'] = 'wenjianyi'
        // 不可移到同一父级
        if (this.formData.pid === id) {
          node['disabled'] = true
          node['text'] = node['name']
          node['icon'] = 'wenjianyi'
        }
        if (this.formData.id === id) {
          srcNode.push(node)
        }
        // add
        pnode.push(node)
        nodeMap.set(node.id, node)
        // next
        table.next()
      }
      // 不可移到自己和下级分组
      //   if (srcNode.length > 0) { this.setUnSelected(srcNode) }
      this.treeData = nodes
    },
    confirm() {
      const node = this.$refs.tree.getCurrentNode()
      if (node === null) {
        this.$message({
          message: '请选择分组！',
          type: 'warning'
        })
        return
      }
      if (this.params.confirm !== undefined) {
        this.params.confirm(node)
      } else {
        this.$emit('confirm', node)
      }
    }
  }
}
</script>
